package com.example.oms.service.impl;

import com.example.oms.dao.DiagRecordsDao;
import com.example.oms.dao.DiagReportsDao;
import com.example.oms.dao.PatientsDao;
import com.example.oms.dao.PrescriptionsDao;
import com.example.oms.entity.Patients;
import com.example.oms.service.CashierService;
import com.example.oms.vo.DiagReportsInfoVo;
import com.example.oms.vo.PayItem;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
public class CashierServiceImpl implements CashierService {

    @Resource
    DiagReportsDao diagReportsDao;
    @Resource
    PrescriptionsDao prescriptionsDao;
    @Resource
    DiagRecordsDao diagRecordsDao;
    @Resource
    PatientsDao patientsDao;
    @Override
    public int chargeConfirm(int diagId) {
        int diagReportChargeConfirm = diagReportsDao.changeDiagReportStatusByDiagId(diagId,"Paid");
        int prescriptionChargeConfirm = prescriptionsDao.changePrescriptionStatusByDiagId(diagId,"Paid");
        return diagReportChargeConfirm+prescriptionChargeConfirm;
    }

    @Override
    public int chargeCancel(int diagId,String category) {
        if(category.equals("药品"))
            return prescriptionsDao.changePrescriptionStatusByDiagId(diagId,"Canceled");
        else
            return diagReportsDao.changeDiagReportStatusByDiagId(diagId,"Canceled");
    }

    @Override
    public List<PayItem> getAllPaidByDiagId(int diagId) {
        List<PayItem> res = new ArrayList<>();
        List<DiagReportsInfoVo> diagReportsInfoVoList = diagReportsDao.getAllPaidByDiagId(diagId);
        for (DiagReportsInfoVo diagReportsInfoVo : diagReportsInfoVoList) {
            PayItem payItem = new PayItem(diagReportsInfoVo.getId(),diagReportsInfoVo.getDiagItemName(),
                    "次",1,diagReportsInfoVo.getPrice(),"检查检验");
            res.add(payItem);
        }
        List<PayItem> medsPayItemList = prescriptionsDao.getAllPaidMedsByDiagId(diagId);
        for (PayItem payItem : medsPayItemList) {
            payItem.setCategory("药品");
            res.add(payItem);
        }
        return res;
    }

    @Override
    public Patients getPatientByDiagId(int diagId) {
        int patientId = diagRecordsDao.getDiagRecordsById(diagId).getPatientId();
        return patientsDao.queryById(patientId);
    }

    @Override
    public List<PayItem> getAllUnpaidByDiagId(int diagId) {
        List<PayItem> res = new ArrayList<>();
        List<DiagReportsInfoVo> diagReportsInfoVoList = diagReportsDao.getAllUnpaidByDiagId(diagId);
        for (DiagReportsInfoVo diagReportsInfoVo : diagReportsInfoVoList) {
            PayItem payItem = new PayItem(diagReportsInfoVo.getId(),diagReportsInfoVo.getDiagItemName(),
                    "次",1,diagReportsInfoVo.getPrice(),"检查检验");
            res.add(payItem);
        }
        List<PayItem> medsPayItemList = prescriptionsDao.getAllUnpaidByDiagId(diagId);
        for (PayItem payItem : medsPayItemList) {
            payItem.setCategory("药品");
            res.add(payItem);
        }
        return res;
    }

    @Override
    public void updateStatus(int diagId) {
        diagReportsDao.updateStatus(diagId);
    }
}
